/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: TalkController
 * Author:   臧浩鹏
 * Date:     2018/8/8 8:10
 * Description: 有关评论的controller层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.etc.controller;

import com.etc.common.ServerResponse;
import com.etc.entity.Talk;
import com.etc.entity.User;
import com.etc.service.TalkService;
import com.etc.utils.DateTimeUtil;
import com.etc.vo.TalkVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 〈一句话功能简述〉<br> 
 * 〈有关评论的controller层〉
 *
 * @author 臧浩鹏
 * @create 2018/8/8
 * @since 1.0.0
 */
@Controller
@RequestMapping("/Talk")
public class TalkController {

    @Autowired
    private TalkService talkService;

    @RequestMapping(value = "/addcomment.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<TalkVo> addComment(@RequestBody() Talk talk, HttpSession session){
        ServerResponse<TalkVo> response = null;
        User user = (User)session.getAttribute("USER_NAME");
        // TODO talk.settUid(user.getUid()) 用户的id 从session获取;
        talk.settUid(3);
        if (talk.getFathertime() == null||talk.getTtopicid()==null){
            String fathertime = "";
            Integer fatherId = 0;
            response = talkService.insertTalk(talk, fatherId, fathertime);
        }else {
            String fathertime = DateTimeUtil.dateToStr(talk.getFathertime());
            response = talkService.insertTalk(talk, talk.getTtopicid(), fathertime);
        }
        return response;
    }

    @RequestMapping("/delcomment.do")
    @ResponseBody
    public ServerResponse<Integer> delComment(Integer tid){
        return talkService.deleteTalk(tid);
    }


    @RequestMapping(value = "/searchcomment.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<TalkVo> searComment(@RequestParam("talk") String talk1, @RequestParam("isAll") boolean isAll) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Talk talk = mapper.readValue(talk1, Talk.class);
        ServerResponse<TalkVo> res = talkService.searchTalkAll(talk.getTcategory(),talk.getTarticleid(),talk.gettUid(), DateTimeUtil.dateToStr(talk.getTcreatetime()),isAll);
        return res;
    }

    @RequestMapping(value = "/searchOnecomment.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<TalkVo> searOneComment(@RequestParam("tid") String tid) throws IOException {
        ServerResponse<Talk> response = talkService.selectByTid(tid);
        if (response.isSuccess()){
            Talk talk = response.getData();
            ServerResponse<TalkVo> res = talkService.searchTalkAll(talk.getTcategory(),talk.getTarticleid(),talk.gettUid(), DateTimeUtil.dateToStr(talk.getTcreatetime()),false);
            return res;
        }
        return ServerResponse.createByErrorMessage(response.getMsg());
    }

}
